const cp = require('cp');

require('./page.css');
// function add_zero(m) {
//   return m < 10 ? '0' + m : m;
// };
// function time(time_num) {
//   let time = new Date(time_num);
//   let year = time.getFullYear();
//   let month = time.getMonth() + 1;
//   let date = time.getDate();
//   // let hour = time.getHours();
//   // let minute = time.getMinutes();
//   return year + '-' + add_zero(month) + '-' + add_zero(date);
// }
let img = [
  require('./images/Group1.png'),
  require('./images/Group2.png'),
  require('./images/Group3.png'),
  require('./images/Group4.png'),
  require('./images/Group5.png'),
  require('./images/Group6.png'),
  require('./images/Group7.png'),
  require('./images/Group8.png'),
  require('./images/Group9.png'),
  require('./images/Group10.png'),
];
let moduleExport = {};
let vm = avalon.define({
  $id: 'mc_decoration',
  shop_info: '',
  shop_temps: [],
  choice_color: [
    {
      type: 1,
      src: img[0],
    },
    {
      type: 2,
      src: img[1],
    },
    {
      type: 3,
      src: img[2],
    },
    {
      type: 4,
      src: img[3],
    },
    {
      type: 5,
      src: img[4],
    },
    {
      type: 6,
      src: img[5],
    },
    {
      type: 7,
      src: img[6],
    },
    {
      type: 8,
      src: img[7],
    },
    {
      type: 9,
      src: img[8],
    },
    {
      type: 10,
      src: img[9],
    },
  ],
  effect: [
    { title: '投票', checked: false },
    { title: '问卷', checked: false },
    { title: '抽奖', checked: false },
    { title: '文章', checked: false },
    { title: '商品', checked: false },
    { title: '服务商', checked: false },
  ],
  formatTime: (time, type, sec) => {
    return cp.formatTime(time, type, sec);
  },
  bindswitch_color: (type, id) => {
    let styleid = 0;
    let shoptempid = 0;
    if (type === 'styleid') {
      styleid = id;
    } else if (type === 'shoptempid') {
      shoptempid = id;
    }
    cp.ajaxUrl({
      controller: 'shopinfo',
      cmd: 'com.rel.porg.pb.shop_design_req',
      data: {
        req: 1,
        shop: {
          styleid: styleid,
          shoptempid: shoptempid,
        },
      },
      success: res => {
        vm.init();
        cp.layerMsg(res.desc);
      },
      fail: res => {
        cp.layerMsg(res.desc);
      },
    });
  },
  init: () => {
    cp.ajaxUrl({
      controller: 'shopinfo',
      cmd: 'com.rel.porg.pb.shop_design_req',
      async: false, // 请使用同步
      data: {
        req: 2,
      },
      success: res => {
        console.log(res);
        vm.shop_info = res.shop;
        vm.shop_temps = res.temps;
        for (let i in res.temps) {
          if (res.temps[i].id === res.shop.shoptempid) {
            let arr = res.temps[i];
            res.temps.remove(res.temps[i]);
            res.temps.unshift(arr);
          }
        }
      },
      fail: res => {
        cp.layerMsg(res.desc);
      },
    });
  },
});
vm.init();
moduleExport.content = require('./content.html');
moduleExport.vm = vm;
module.exports = moduleExport;
